package io.renren.common.utils;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Arrays;
import java.util.Map;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipUtils {

    public static void getZipFile(Map<String, byte[]> stringByte, OutputStream outputStream, HttpServletResponse response) throws Exception {
        //将文件打包成压缩文件
        ZipOutputStream zipout = new ZipOutputStream(outputStream);
        Set<Map.Entry<String, byte[]>> dataEntrys = stringByte.entrySet();
        InputStream bufferIn = null;
        for (Map.Entry<String, byte[]> data : dataEntrys) {
            bufferIn = new BufferedInputStream(new ByteArrayInputStream(data.getValue()));

            byte[] bs = new byte[1024];
            Arrays.fill(bs, (byte) 0);
            //创建压缩文件内的文件
            zipout.putNextEntry(new ZipEntry(data.getKey()));
            int len = 0;
            while ((len = bufferIn.read(bs)) > 0) {
                zipout.write(bs, 0, len);
                zipout.flush();
            }
            response.flushBuffer();
            bufferIn.close();
        }
        try {
            zipout.close();
            outputStream.flush();
        } catch (IOException e) {
        }
    }

}


